﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Includes;

namespace Arztmodul
{
    /// <summary>
    /// Version: 1.1
    ///  Author: Jan Baltzer
    ///    Date: 2012-06-29
    /// form to edit or create patient
    /// </summary>
    public partial class EditCreate : Form
    {
        //classGlobal variables
        private IPersistDoc persist;
        private Patient patient;


        /// <summary>
        /// constructor to edit and create
        /// </summary>
        /// <param name="patient">patient to work with</param>
        /// <param name="persi">concrete BackEnd to work with</param>
        public EditCreate(Patient patient, IPersistDoc persi)
        {
            InitializeComponent();

            //save BackEnd
            this.persist = persi;

            //save Patiend
            this.patient = patient;

            //set Name of patient
            this.firstNameField.Text = this.patient.firstName;
            this.nameField.Text = this.patient.name;

            //set default data of patient
            initializeTrackBars();
            initializeComboBoxes();
            this.checkTimePicker1.Value = this.patient.checkTime1;
            this.checkTimePicker2.Value = this.patient.checkTime2;
            this.dateOfbirthPicker.Value = DateTime.Now;


            //check kr, if it isnt, hide krButton
            if (!this.patient.kr)
            {
                this.krButton.Hide();
            }

            //check if patient is full initialized: yes: fill fields
            if (this.patient.isValidPatient())
            {
                this.dateOfbirthPicker.Value = this.patient.dateOfBirth;
                this.diagnosisField.Text = this.patient.diagnosis;
                this.telephoneField.Text = new string(this.patient.telephoneNumber.ToCharArray(3, this.patient.telephoneNumber.Length - 3));
                this.secondTelephoneField.Text = new string(this.patient.secondTelephoneNumber.ToCharArray(3, this.patient.secondTelephoneNumber.Length - 3));
                this.normalBloodPressureField.Text = this.patient.normalBloodPressure.ToString();
                this.normalPulseField.Text = this.patient.normalPulse.ToString();
                this.normalWeightField.Text = this.patient.normalWeight.ToString();

                //redraw headline
                this.Text = "Editieren";
            }
        }

        /// <summary>
        /// initialize all trackBars
        /// </summary>
        private void initializeTrackBars()
        {
            //initialize Tracks
            this.acceptableBloodPressureTrackBar.Value = this.patient.acceptableBloodPressure;
            this.criticalBloodPressureTrackBar.Value = this.patient.criticalBloodPressure;
            this.acceptablePulseTrackBar.Value = this.patient.acceptablePulse;
            this.criticalPulseTrackBar.Value = this.patient.criticalPulse;
            this.acceptableWeightTrackBar.Value = this.patient.acceptableWeight;
            this.criticalWeightTrackBar.Value = this.patient.criticalWeight;

            //initialize TrackValues
            acceptableBloodPressureTrackBar_Scroll(null, null);
            criticalBloodPressureTrackBar_Scroll(null, null);
            acceptablePulseTrackBar_Scroll(null, null);
            criticalPulseTrackBar_Scroll(null, null);
            acceptableWeightTrackBar_Scroll(null, null);
            criticalWeightTrackBar_Scroll(null, null);
        }

        /// <summary>
        /// initialize all comboBoxes
        /// </summary>
        private void initializeComboBoxes()
        {
            initializeGenderComboBox();
            initializeStatusComboBox();
        }

        /// <summary>
        /// initializes genderComboBox
        /// </summary>
        private void initializeGenderComboBox()
        {
            this.genderComboBox.Items.Add(Gender.Keins);
            this.genderComboBox.Items.Add(Gender.Männlich);
            this.genderComboBox.Items.Add(Gender.Weiblich);

            this.genderComboBox.SelectedIndex = this.genderComboBox.FindString(patient.gender.ToString());
        }

        /// <summary>
        /// initializes statusComboBox
        /// </summary>
        private void initializeStatusComboBox()
        {
            this.statusComboBox.Items.Add(Status.Keiner);
            this.statusComboBox.Items.Add(Status.Aktiv);
            this.statusComboBox.Items.Add(Status.Inaktiv);

            this.statusComboBox.SelectedIndex = this.statusComboBox.FindString(patient.status.ToString());
        }

        /// <summary>
        /// to refresh patient
        /// </summary>
        private void refreshPatient()
        {
            this.patient.name = this.nameField.Text;
            this.patient.firstName = this.firstNameField.Text;
            this.patient.dateOfBirth = this.dateOfbirthPicker.Value;
            this.patient.diagnosis = this.diagnosisField.Text; 
            this.patient.telephoneNumber = this.telephoneField.Text.Replace(" ","");
            this.patient.secondTelephoneNumber = this.secondTelephoneField.Text.Replace(" ","");
            
            try { this.patient.normalBloodPressure = Convert.ToInt32(this.normalBloodPressureField.Text.Replace(" ","")); }
            catch(Exception) { this.patient.normalBloodPressure = 0; }

            this.patient.acceptableBloodPressure = this.acceptableBloodPressureTrackBar.Value;
            this.patient.criticalBloodPressure = this.criticalBloodPressureTrackBar.Value;
            
            try { this.patient.normalPulse = Convert.ToInt32(this.normalPulseField.Text.Replace(" ", "")); }
            catch (Exception) { this.patient.normalPulse = 0; }
            
            this.patient.acceptablePulse = this.acceptablePulseTrackBar.Value;
            this.patient.criticalPulse = this.criticalPulseTrackBar.Value;
            
            try { this.patient.normalWeight = Convert.ToInt32(this.normalWeightField.Text.Replace(" ", "")); }
            catch (Exception) { this.patient.normalWeight = 0; }
            
            this.patient.acceptableWeight = this.acceptableWeightTrackBar.Value;
            this.patient.criticalWeight = this.criticalWeightTrackBar.Value;
            this.patient.checkTime1 = this.checkTimePicker1.Value;
            this.patient.checkTime2 = this.checkTimePicker2.Value;
            patient.gender = ((Gender)genderComboBox.SelectedItem);
            patient.status = ((Status)statusComboBox.SelectedItem);
        }

        /// <summary>
        /// routine to handle backButton_Click event
        /// </summary>
        /// <param name="sender">sender of event</param>
        /// <param name="e">event</param>
        private void backButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// routine to handle saveButton_Click event
        /// </summary>
        /// <param name="sender">sender of event</param>
        /// <param name="e">event</param>
        private void saveButton_Click(object sender, EventArgs e)
        {
            //refresh full patient
            this.refreshPatient();

            //check if input is correct
            if(this.patient.isValidPatient())
            {
               //try to store patient to database and close
               try
               {
                   this.persist.setPatient(this.patient);
               }
               catch (Exception)
               {
                  //show warning
                  MessageBox.Show(HCSWarnings.MESSAGE_BOX_WARNING_NO_DATABASE, HCSWarnings.MESSAGE_BOX_WARNING_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Warning);
               }
               finally
               {
                   Close();
               }
            }
            else
            {
               //show warning
               MessageBox.Show(HCSWarnings.MESSAGE_BOX_WARNING_BAD_USER, HCSWarnings.MESSAGE_BOX_WARNING_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// routine to handle acceptableBloodPressureTrackBar_Scroll event
        /// </summary>
        /// <param name="sender">sender of event</param>
        /// <param name="e">event</param>
        private void acceptableBloodPressureTrackBar_Scroll(object sender, EventArgs e)
        {
            if(this.acceptableBloodPressureTrackBar.Value > this.criticalBloodPressureTrackBar.Value)
            {
                this.criticalBloodPressureTrackBar.Value = this.acceptableBloodPressureTrackBar.Value;
                this.criticalBloodPressureField.Text = this.criticalBloodPressureTrackBar.Value.ToString() + "%";
            }
            this.acceptableBloodPressureField.Text = this.acceptableBloodPressureTrackBar.Value.ToString() + "%";
        }

        /// <summary>
        /// routine to handle criticalBloodPressureTrackBar_Scroll event
        /// </summary>
        /// <param name="sender">sender of event</param>
        /// <param name="e">event</param>
        private void criticalBloodPressureTrackBar_Scroll(object sender, EventArgs e)
        {
            if(this.criticalBloodPressureTrackBar.Value < this.acceptableBloodPressureTrackBar.Value)
            {
                this.acceptableBloodPressureTrackBar.Value = this.criticalBloodPressureTrackBar.Value;
                this.acceptableBloodPressureField.Text = this.acceptableBloodPressureTrackBar.Value.ToString() + "%";
            }
            this.criticalBloodPressureField.Text = this.criticalBloodPressureTrackBar.Value.ToString() + "%";
        }

        /// <summary>
        /// routine to handle acceptablePulseTrackBar_Scroll event
        /// </summary>
        /// <param name="sender">sender of event</param>
        /// <param name="e">event</param>
        private void acceptablePulseTrackBar_Scroll(object sender, EventArgs e)
        {
            if (this.acceptablePulseTrackBar.Value > this.criticalPulseTrackBar.Value)
            {
                this.criticalPulseTrackBar.Value = this.acceptablePulseTrackBar.Value;
                this.criticalPulseField.Text = this.criticalPulseTrackBar.Value.ToString() + "%";
            }
            this.acceptablePulseField.Text = this.acceptablePulseTrackBar.Value.ToString() + "%";
        }

        /// <summary>
        /// routine to handle criticalPulseTrackBar_Scroll event
        /// </summary>
        /// <param name="sender">sender of event</param>
        /// <param name="e">event</param>
        private void criticalPulseTrackBar_Scroll(object sender, EventArgs e)
        {
            if (this.criticalPulseTrackBar.Value < this.acceptablePulseTrackBar.Value)
            {
                this.acceptablePulseTrackBar.Value = this.criticalPulseTrackBar.Value;
                this.acceptablePulseField.Text = this.acceptablePulseTrackBar.Value.ToString() + "%";
            }
            this.criticalPulseField.Text = this.criticalPulseTrackBar.Value.ToString() + "%";
        }

        /// <summary>
        /// routine to handle acceptableWeightTrackBar_Scroll event
        /// </summary>
        /// <param name="sender">sender of event</param>
        /// <param name="e">event</param>
        private void acceptableWeightTrackBar_Scroll(object sender, EventArgs e)
        {
            if (this.acceptableWeightTrackBar.Value > this.criticalWeightTrackBar.Value)
            {
                this.criticalWeightTrackBar.Value = this.acceptableWeightTrackBar.Value;
                this.criticalWeightField.Text = this.criticalWeightTrackBar.Value.ToString() + "%";
            }
            this.acceptableWeightField.Text = this.acceptableWeightTrackBar.Value.ToString() + "%";
        }

        /// <summary>
        /// routine to handle criticalWeightTrackBar_Scroll event
        /// </summary>
        /// <param name="sender">sender of event</param>
        /// <param name="e">event</param>
        private void criticalWeightTrackBar_Scroll(object sender, EventArgs e)
        {
            if (this.criticalWeightTrackBar.Value < this.acceptableWeightTrackBar.Value)
            {
                this.acceptableWeightTrackBar.Value = this.criticalWeightTrackBar.Value;
                this.acceptableWeightField.Text = this.acceptableWeightTrackBar.Value.ToString() + "%";
            }
            this.criticalWeightField.Text = this.criticalWeightTrackBar.Value.ToString() + "%";
        }

        /// <summary>
        /// routine to handle krButton_Click event
        /// </summary>
        /// <param name="sender">sender of event</param>
        /// <param name="e">event</param>
        private void krButton_Click(object sender, EventArgs e)
        {
            //reset kr
            this.patient.kr = false;

            //hide krButton
            this.krButton.Hide();
        }
    }
}
